package edu.sctu.xuping.month06.goodstest;

import java.io.*;

/**
 * Created by user02 on 2015/6/28.
 */
public class FileUtil {


    /**
     * 创建文件夹
     *
     * @param folder_path
     */
    public void createFolder(String folder_path) {
        File file = new File(folder_path);
        if (!file.exists()) {
            file.mkdir();
        }
    }


    /**
     * 创建json文件并将获取到的数据写入文件
     *
     * @param file_path1
     * @throws IOException
     */
    public void saveJsonFile(String file_path1,String jsondata) throws IOException {

        File file1 = new File(file_path1);
        FileWriter fileWriter = new FileWriter(file1);
        if (!file1.exists()) {
            file1.createNewFile();
        }
        fileWriter.write(jsondata);
        fileWriter.close();

    }

    /**
     * 创建Appbean文件
     * @param file_path2
     * @throws IOException
     */

    public void createAppbeanFile(String file_path2) throws IOException {

        File file2 = new File(file_path2);
        if (!file2.exists()) {
            file2.createNewFile();
        }

    }

    /**
     * 将获取到的数据转换为实体类
     * @param File_Path2
     * @param goodsAppbean
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public GoodsAppbean appbeanClassTransfor(String File_Path2,GoodsAppbean goodsAppbean) throws IOException, ClassNotFoundException {

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(File_Path2));
        objectOutputStream.writeObject(goodsAppbean);
        objectOutputStream.close();//序列化

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(File_Path2));
        GoodsAppbean goodsAppbean2 = (GoodsAppbean) objectInputStream.readObject();
        objectInputStream.close();//反序列化

        return goodsAppbean2;

    }


}
